package com.hcrazy.manchester.app.controllers;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.LiveSort;
import com.hcrazy.manchester.app.services.LiveSortService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

/**
 * 生活信息分类管理
 * @author Hsmart
 *
 */
@Controller
@RequestMapping("/admin/live")
public class LiveSortController {
	
	@Autowired
	private LiveSortService liveSortService;
	
	@RequestMapping(value="/live_sort",method=RequestMethod.GET)
	public void live_sort(){
		
	}
	
	/**
	 * 跳转到添加子分类页面
	 * 初始化数据
	 * @param sortParent
	 * @param model
	 */
	@RequestMapping(value="/addChildSort",method=RequestMethod.GET)
	public void addChildSort(@RequestParam int sortParent,Model model){
		//根据父分类id获取父分类对象
		try {
			LiveSort parentSort = new LiveSort();
			if(sortParent == 0){
				parentSort.setSortId(0);
				parentSort.setSortName("无");
			}else{
				parentSort = liveSortService.getSortById(sortParent);
			}
			model.addAttribute("parentSort",parentSort);
			LiveSort liveSort = new LiveSort();
			model.addAttribute("liveSort",liveSort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
	}
	
	/**
	 * 跳转到编辑分类页面
	 * @param sortId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editChildSort",method=RequestMethod.GET)
	public String editChildSort(@RequestParam int sortId,Model model){
		
		try {
			LiveSort liveSort = liveSortService.getSortById(sortId);
			addChildSort(liveSort.getSortParent(),model);
			
			model.addAttribute("liveSort",liveSort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		
		return "/admin/live/addChildSort";
	}
    
	/**
	 * 删除分类
	 * @param sortId
	 * @return
	 */
	@RequestMapping(value="/delChildSort",method=RequestMethod.GET)
	public String delLiveSort(@RequestParam int sortId,Model model){
		try {
			String msg = liveSortService.delLiveSort(sortId);
			if(null != msg){
				model.addAttribute("message", msg);
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/live/live_sort";
	}
	
	/**
	 * 提交分类信息
	 * @param sort
	 * @return
	 */
	@RequestMapping(value="/sort",method=RequestMethod.POST)
    public String addOrUpdateSort(@ModelAttribute("form") LiveSort sort,
    		Model model){
		try {
			liveSortService.addOrUpdateLiveSort(sort);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "/admin/live/live_sort";
    }
	
	/**
	 * 获取全部分类（树状结构）
	 * @return
	 */
	@RequestMapping(value="/sorts",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<LiveSort> getAllLiveSorts(){
		
		try {
			return liveSortService.getAllSorts(Constant.TYPE_LIVE,0);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
}
